<template>
  <div>
    <h1>操作人员添加</h1>
    <el-form :model="admin" label-width="120px">
      <el-form-item label="姓名">
        <el-input v-model="admin.adminname" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="admin.password" />
      </el-form-item>
      <el-form-item label="角色">
        <el-radio-group v-model="admin.role">
          <el-radio :label="1">超级管理员</el-radio>
          <el-radio :label="2">普通管理员</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addAdmin">添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
<script setup lang="ts">
import { ElMessage } from "element-plus";
import { reactive } from "vue";
import service from "../../../utils/service";

// 在setup函数里（setup语法糖里），如果想使用路由对象。
import { useRouter } from "vue-router";

const $router = useRouter();

interface IAdmin {
  adminname: string;
  password: string;
  role: number; //1:表示超级管理员，2：普通管理员
  checkedKeys: string[];
}

// 当前管理员的数据
const admin = reactive<IAdmin>({
  adminname: "",
  password: "",
  role: 1,
  checkedKeys: [],
});

const addAdmin = () => {
  service({
    url: "/admin/add",
    method: "post",
    data: admin,
  }).then((res) => {
    if (res.data.code === "200") {
      ElMessage({
        message: "亲，恭喜您，添加成功！",
        type: "success",
      });
      $router.push("/Admin/AdminList");
    }
  });
};
</script>
  
<style></style>